//
//  BigTableView.m
//  bigtableview
//
//  Created by GripMeMory on 16/1/7.
//  Copyright © 2016年 GripMeMory. All rights reserved.
//

#import "BigTableView.h"
#import "TableViewDataSource.h"
#import "BigTableViewLeftCell.h"
#import "BigTableViewRightCell.h"
#import "BigTableViewModal.h"

static NSString * const cellIdentifier = @"rightCell";
static NSString * const cellIdentifier2 = @"leftCell";


@interface BigTableView () <UITableViewDelegate, UIScrollViewDelegate>

//右侧数据源
@property (nonatomic, strong) TableViewDataSource *dataSource1;
//左侧数据源
@property (nonatomic, strong) TableViewDataSource *dataSource2;

@end

@implementation BigTableView

{
    UIView *_headerView;//右侧表头
    UIView *_leftHeaderView;//左侧表头
    UIScrollView *_mainScrollView;//右侧背景ScrollView
    NSMutableArray *_leftTitlesLabelArray;//左侧表头列表暂存
    NSMutableArray *_titlesLabelArray;//右侧表头列表暂存
    
    NSMutableArray *_contentArray;
    NSMutableArray *_contentArray2;
}

#pragma mark - View Initialize Method

- (instancetype)init{
    self = [super init];
    if (self) {
        
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame HeaderTitles:(NSArray *)headerTitles ColumnCount:(int)columnCount RowHeadingCount:(int)rowHeadingCount ColumnWidth:(float)columnWidth RowHeight:(CGFloat)rowHeight{
    self = [super initWithFrame:frame];
    if (self) {
        
        _titlesLabelArray = [NSMutableArray arrayWithCapacity:10];
        _leftTitlesLabelArray = [NSMutableArray arrayWithCapacity:10];
        self.headerTiltes = headerTitles;
        
        _rowHeadingCount = rowHeadingCount;
        _columnCount = columnCount;
        _columnWidth = columnWidth;
        _rowHeight = rowHeight;
        
        [self fakeValue];
        [self drawBackScrollView];
        [self drawHeaderView];
        [self drawLeftHeaderView];
        [self setupRightTableView];
        [self setupLeftTableView];
    }
    return self;
}

- (void)fakeValue{
    
    _contentArray = [[NSMutableArray alloc] initWithCapacity:15];
    
    for (int i = 0; i < 15; i++) {
        BigTableViewModal *modal = [[BigTableViewModal alloc] init];
        NSArray *array = @[@"限时报价期", @"52天11小时24分32秒", @"1,000,000", @"您的出价为990,00", @"正向多次", @"20", [NSString stringWithFormat:@"A%03d", i]];
        modal.contentArray = array;
        modal.state = arc4random() % 3;
        [_contentArray addObject:modal];
    }
    
    
    _contentArray2 = [NSMutableArray arrayWithCapacity:15];
    
    for (int i = 0; i < _contentArray.count; i++) {
        BigTableViewModal *modal = [[BigTableViewModal alloc] init];
        NSArray *array = @[[NSString stringWithFormat:@"标的名称%03d", i]];
        modal.contentArray = [array copy];
        modal.state = [(BigTableViewModal *)_contentArray[i] state];
        [_contentArray2 addObject:modal];
    }
    
}

#pragma mark - View Life Cycle

- (void)layoutSubviews{
    _mainScrollView.frame = CGRectMake(_rowHeadingCount * _columnWidth, 0, self.frame.size.width - _columnWidth * _rowHeadingCount, self.frame.size.height);
    _headerView.frame = CGRectMake(0, _mainScrollView.bounds.origin.y, (_columnCount - _rowHeadingCount) * _columnWidth, _rowHeight);
    _mainScrollView.bounces = NO;
    
    _headerView.opaque = 0;
    
    _mainScrollView.contentSize = CGSizeMake(_columnWidth * (_columnCount - _rowHeadingCount), 0);
    _mainScrollView.userInteractionEnabled = YES;
    
    _leftHeaderView.frame = CGRectMake(0, 0, _rowHeadingCount * _columnWidth, _rowHeight);
    
    for (int i = 0; i < _columnCount - _rowHeadingCount; i++) {
        UILabel *label = _titlesLabelArray[i];
        label.frame = CGRectMake(i * _columnWidth, 0, _columnWidth, _rowHeight);
        label.text = _headerTiltes[_rowHeadingCount + i];
        label.textAlignment = NSTextAlignmentCenter;
    }
    
    for (int i = 0; i < _rowHeadingCount; i++) {
        UILabel *label = _leftTitlesLabelArray[i];
        label.frame = CGRectMake(i * _columnWidth, 0, _columnWidth, _rowHeight);
        label.text = _headerTiltes[i];
        label.textAlignment = NSTextAlignmentCenter;
    }
    
    _rightTableView.frame = CGRectMake(_headerView.frame.origin.x, _mainScrollView.bounds.origin.y + _headerView.frame.size.height, _headerView.frame.size.width, _mainScrollView.height - _rowHeight);
    
    _leftTableView.frame = CGRectMake(_leftHeaderView.frame.origin.x, self.bounds.origin.y + _leftHeaderView.frame.size.height, _leftHeaderView.frame.size.width, _mainScrollView.height - _rowHeight);
}

#pragma mark - CustomMainView

/**
 *  @author GripMeMory, 16-01-08 10:01:38
 *
 *  设置右侧背景ScrollView
 */
- (void)drawBackScrollView{
    _mainScrollView = [[UIScrollView alloc] init];
    _mainScrollView.backgroundColor = [UIColor lightGrayColor];
    _mainScrollView.scrollEnabled = YES;
    [self addSubview:_mainScrollView];
}

/**
 *  @author GripMeMory, 16-01-08 10:01:54
 *
 *  绘制右侧表头
 */
- (void)drawHeaderView{
    _headerView = [[UIView alloc] init];
    _headerView.backgroundColor = RGB_COLOR(@"#545454");
    
    for (int i = 0; i < _columnCount - _rowHeadingCount; i++) {
        UILabel *label = [[UILabel alloc] init];
        label.text = _headerTiltes[_rowHeadingCount + i];
        label.textAlignment = NSTextAlignmentCenter;
        label.tag = i;
        label.textColor = [UIColor whiteColor];
        [_headerView addSubview:label];
        [_titlesLabelArray addObject:label];
    }
    
    [_mainScrollView addSubview:_headerView];
}

/**
 *  @author GripMeMory, 16-01-08 14:01:49
 *
 *  绘制左侧表头
 */
- (void)drawLeftHeaderView{
    _leftHeaderView = [[UIView alloc] init];
    _leftHeaderView.backgroundColor = RGB_COLOR(@"#545454");
    
    for (int i = 0; i < _rowHeadingCount; i++) {
        UILabel *label = [[UILabel alloc] init];
        label.text = _headerTiltes[i];
        label.textAlignment = NSTextAlignmentCenter;
        label.tag = 100 + i;
        label.textColor = [UIColor whiteColor];
        [_leftHeaderView addSubview:label];
        [_leftTitlesLabelArray addObject:label];
    }
    
    [self addSubview:_leftHeaderView];
}

/**
 *  @author GripMeMory, 16-01-08 10:01:23
 *
 *  绘制右侧列表
 */
- (void)setupRightTableView{
    
    TableViewCellConfigurBlock cellConfigureBlock = ^(BigTableViewRightCell *cell, BigTableViewModal *modal){
        [cell configureWithModal:modal];
    };
    _rightTableView = [[UITableView alloc] init];
    _rightTableView.tag = 1;
    [_rightTableView registerClass:[BigTableViewRightCell class] forCellReuseIdentifier:cellIdentifier];
    _dataSource1 = [[TableViewDataSource alloc] initWithItems:_contentArray cellIdentifier:cellIdentifier configureBlock:cellConfigureBlock];
    _rightTableView.delegate = self;
    _rightTableView.dataSource = _dataSource1;
    _rightTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _rightTableView.backgroundColor = RGB_COLOR(@"#112530");
    
    
    
    
    [_mainScrollView addSubview:_rightTableView];
}

/**
 *  @author GripMeMory, 16-01-08 14:01:33
 *
 *  绘制左侧列表
 */
- (void)setupLeftTableView{
    
    TableViewCellConfigurBlock cellConfigureBlock2 = ^(BigTableViewLeftCell *cell, BigTableViewModal *modal){
        [cell configureWithModal:modal];
    };
    _leftTableView = [[UITableView alloc] init];
    _leftTableView.tag = 2;
    _leftTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_leftTableView registerClass:[BigTableViewLeftCell class] forCellReuseIdentifier:cellIdentifier2];
    _dataSource2 = [[TableViewDataSource alloc] initWithItems:_contentArray2 cellIdentifier:cellIdentifier2 configureBlock:cellConfigureBlock2];
    
    for (BigTableViewModal *modal in _contentArray2) {
        NSLog(@"%@", modal.contentArray.firstObject);
    }
    
    _leftTableView.delegate = self;
    _leftTableView.dataSource = _dataSource2;
    _leftTableView.backgroundColor = RGB_COLOR(@"#112530");
    [self addSubview:_leftTableView];
}

#pragma mark - Table View Delegate

/**
 *  @author GripMeMory, 16-01-08 10:01:44
 *
 *  返回行高
 *
 */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return _rowHeight;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView.tag == 1) {
        [_leftTableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
    }else{
        [_rightTableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
    }
}

#pragma mark - Scrol View Delegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat offsetY = _rightTableView.contentOffset.y;
    CGPoint leftOffSetY = _leftTableView.contentOffset;
    leftOffSetY.y = offsetY;
    _leftTableView.contentOffset = leftOffSetY;
    if (offsetY == 0) {
        _leftTableView.contentOffset = CGPointZero;
    }
}

@end
